﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;
using static OpenIddict.Abstractions.OpenIddictConstants;

namespace WMSProject.ChuBoWen
{
    /// <summary>
    /// 销售管理服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class SalesService : ApplicationService, ISalesService
    {
        private readonly IRepository<Sales, int> _salesRepository;
        private readonly IRepository<Products, int> _productsRepository;
        private readonly IRepository<SaleDetils, int> _saleDetilsRepository;
        public SalesService(IRepository<Sales, int> salesRepository, IRepository<Products, int> productsRepository, IRepository<SaleDetils, int> saleDetilsRepository)
        {
            _salesRepository = salesRepository;
            _productsRepository = productsRepository;
            _saleDetilsRepository = saleDetilsRepository;
        }

        /// <summary>
        /// 获取销售列表
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetSalesList(string? salesOrderCode, string? productCode, string? productName)
        {
            var salesList = from a in await _salesRepository.GetListAsync()
                            join b in await _saleDetilsRepository.GetListAsync() on a.SalesOrderCode equals b.SalesOrderCode
                            join c in await _productsRepository.GetListAsync() on b.ProductCode equals c.ProductCode
                            select new
                            {
                                a.SalesOrderCode,
                                c.ProductName,
                                c.ProductCode,
                                c.ProductSpecification,
                                c.UnitPrice,
                                c.Unit,
                                b.Quantity,
                                a.CustomerCode,
                                a.CustomerName,
                                a.OutQuantity
                            };
            if (!string.IsNullOrEmpty(salesOrderCode))
            {
                salesList = salesList.Where(a => a.SalesOrderCode.Contains(salesOrderCode));
            }
            if (!string.IsNullOrEmpty(productCode))
            {
                salesList = salesList.Where(a => a.ProductCode.Contains(productCode));
            }
            if (!string.IsNullOrEmpty(productName))
            {
                salesList = salesList.Where(a => a.ProductName.Contains(productName));
            }
            return new
            {
                code = "0",
                data = salesList,
                count = salesList.Count(),
                msg = "success"
            };
        }

        /// <summary>
        /// 获取产品
        /// </summary>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<object> GetProduct(RequestProductDTO productDTO)
        {
            var productList = await _productsRepository.GetQueryableAsync();
            productList = productList.WhereIf(!string.IsNullOrEmpty(productDTO.ProductCode), x => x.ProductCode.Contains(productDTO.ProductCode));
            productList = productList.WhereIf(!string.IsNullOrEmpty(productDTO.ProductName), x => x.ProductName.Contains(productDTO.ProductName));
            var count = productList.Count();
            productList = productList.OrderBy(x => x.Id).Skip(((productDTO.Page - 1) ?? 0) * (productDTO.Limit ?? 10)).Take(productDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = productList.ToList(),
                msg = "",
            };
            return result;
        }
    }
}
